package org.appFactory.carleton;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.util.ServletContextAware;
import org.appFactory.facebook.FacebookService;
import org.appFactory.facebook.FacebookServiceImpl;
import org.appFactory.shared.Util;

import ognl.OgnlRuntime;


public class CarletonBaseListener extends ActionSupport implements ServletContextListener, ServletContextAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//private static final String APP_URL = "http://cloud-hangout.appspot.com";
	private static final String APP_URL = "http://localhost:8888";
	
	ServletContext context;
	//Ideally there should be a facebookAppService bean which should be initialized with FacebookServiceImpl
	//We just skip that part - which will be later provided by Spring Architecture.
	private FacebookService fbService = new FacebookServiceImpl();
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		OgnlRuntime.setSecurityManager(null);
	}
	
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	
	public FacebookService getFacebookService() {
		Object accessToken = Util.getSessionObj("FB_ACCESS_CODE");
		if(accessToken != null) 
			fbService.setAccessToken(accessToken.toString());	
		return fbService;
	}
	
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String getBaseURL() {
		return APP_URL;
	}

}
